package com.hymjweb.demo.framework.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

public class PinyinUtil {
	/**
	 * 取汉语拼音数组，多音字时，以笛卡尔乘积方式
	 */
	public static String[] getPinYinALLArr(String s, int CaseType) {
		if (DataTypeUtil.isEmptyStr(s))
			return null;
		HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
		outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
		outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		String[] arr = null;
		for (int i = 0; i < s.length(); i++) {
			try {
				char c = s.charAt(i);
				String[] tmpArr = null;
				if (DataTypeUtil.isHasChiness(String.valueOf(c))) {
					tmpArr = PinyinHelper.toHanyuPinyinStringArray(c, outputFormat);
				} else
					tmpArr = new String[] { String.valueOf(c).toLowerCase() };
				if (i == 0) {
					arr = tmpArr;
				} else {
					arr = getDescartes(arr, tmpArr, false, CaseType);
				}
			} catch (Exception e) {
				//NutzUtil.getLogger().error(e.getMessage(), e);
			}
		}
		return arr;
	}

	/***
	 * 取2个字符串数组之间的笛卡尔累加
	 * @param arr1
	 * @param arr2
	 * @return
	 */
	public static String[] getDescartes(String[] arr1, String[] arr2, boolean deleteRepeated, int CaseType) {
		if (DataTypeUtil.isEmptyAll(arr1) && !DataTypeUtil.isEmptyAll(arr2))
			return arr2;
		if (!DataTypeUtil.isEmptyAll(arr1) && DataTypeUtil.isEmptyAll(arr2))
			return arr1;
		String[] arr = new String[arr1.length * arr2.length];
		int index = 0;
		for (int i = 0; i < arr1.length; i++) {
			for (int j = 0; j < arr2.length; j++) {
				String temp1 = arr1[i];
				String temp2 = arr2[j];
				temp2 = getCaseString(temp2, CaseType);
				if (CaseType == GET_PINYINALL_CASE_OnlyFistUpperCase) {
					temp2 = getCaseString(temp2, GET_PINYINALL_CASE_AllLowerCase);
				}
				arr[index] = getCaseString(temp1, CaseType) + temp2;
				index++;
			}
		}
		if (deleteRepeated) {
			arr = DataTypeUtil.deleteRepeatedString(arr);
		}
		return arr;
	}

	private static String getCaseString(String s, int CaseType) {
		if (DataTypeUtil.isEmptyStr(s))
			return s;
		switch (CaseType) {
		case GET_PINYINALL_CASE_AllLowerCase:
			s = s.toLowerCase();
			break;
		case GET_PINYINALL_CASE_AllUpperCase:
			s = s.toUpperCase();
			break;
		case GET_PINYINALL_CASE_AllFistUpperCase:
			s = s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
			break;
		case GET_PINYINALL_CASE_OnlyFistUpperCase:
			s = s.substring(0, 1).toUpperCase() + s.substring(1, s.length());
			break;
		default:
			break;
		}
		return s;
	}

	/**
	 * 调用getPinYinALL方法时返回全小写字母
	 */
	public static final int GET_PINYINALL_CASE_AllLowerCase = 1;

	/**
	 * 调用getPinYinALL方法时返回全大写字母
	 */
	public static final int GET_PINYINALL_CASE_AllUpperCase = 2;

	/**
	 * 调用getPinYinALL方法时返回所有首字母大写，例如李四返回LiSi
	 */
	public static final int GET_PINYINALL_CASE_AllFistUpperCase = 3;

	/**
	 * 调用getPinYinALL方法时返回近首字母大写，例如李四返回Lisi
	 */
	public static final int GET_PINYINALL_CASE_OnlyFistUpperCase = 4;

	/**
	 * 取汉语拼音，多音字时，以笛卡尔乘积方式，然后去重返回，用逗号隔开
	 * @param s
	 * @param CaseType
	 * @return
	 */
	public static String getPinYinALL(String s, int CaseType) {
		String[] arr = getPinYinALLArr(s, CaseType);
		String rets = "";
		if (arr != null && arr.length > 0) {
			arr = DataTypeUtil.deleteRepeatedString(arr);
			rets = DataTypeUtil.arr2str(arr, ",");
		}
		return rets;
	}

	public static String getPinYinFirst(String s) {
		if (DataTypeUtil.isEmptyStr(s))
			return "";
		HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
		outputFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
		outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		String rets = "";
		String[] arr = null;
		for (int i = 0; i < s.length(); i++) {
			try {
				char c = s.charAt(i);
				String[] tmpArr = null;
				String[] tmpArrFirst = null;
				if (DataTypeUtil.isHasChiness(String.valueOf(c))) {
					tmpArr = PinyinHelper.toHanyuPinyinStringArray(c, outputFormat);
					if (tmpArr != null && tmpArr.length > 0) {
						tmpArrFirst = new String[tmpArr.length];
						for (int j = 0; j < tmpArr.length; j++) {
							String pinyin = tmpArr[j];
							if (!DataTypeUtil.isEmptyStr(pinyin)) {
								tmpArrFirst[j] = pinyin.substring(0, 1);
							} else {
								tmpArrFirst[j] = "";
							}
						}
					}
				} else {
					tmpArrFirst = new String[] { String.valueOf(c).toLowerCase() };
				}
				if (i == 0) {
					arr = tmpArrFirst;
				} else {
					arr = getDescartes(arr, tmpArrFirst, false, GET_PINYINALL_CASE_AllUpperCase);
				}
			} catch (Exception e) {
				//NutzUtil.getLogger().error(e.getMessage(), e);
			}
		}
		if (arr != null && arr.length > 0) {
			arr = DataTypeUtil.deleteRepeatedString(arr);
			rets = DataTypeUtil.arr2str(arr, ",");
		}
		return rets;
	}
	
	public static void main(String[] args) {
	    Object[] arr = DataTypeUtil.deleteRepeatedObject(null);
	    for (int i = 0; i < arr.length; i++) {
	        System.out.println(arr[i]);
        }
    }
}
